package com.sec.android.app.myfiles.module.local.recent;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.GridView;
import android.widget.ListAdapter;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.util.UiUtils;

public class RecentGridView extends GridView {
    public RecentGridView(Context context) {
        super(context);
    }

    public RecentGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RecentGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }

    @Override
    public void setChoiceMode(int choiceMode) {
        if (choiceMode == CHOICE_MODE_NONE) {
            clearChoices();

            int cnt = getChildCount();

            for (int i = 0; i < cnt; i++) {
                View view = getChildAt(i);
                if (view instanceof Checkable) {
                    ((Checkable) view).setChecked(false);
                }
            }
        }

        super.setChoiceMode(choiceMode);

        if (!AppFeatures.isSupportIntensity()) {
            super.setHapticFeedbackEnabled(false);
        }
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Parcelable state = onSaveInstanceState();
        ListAdapter adapter = getAdapter();
        setAdapter(adapter);
        onRestoreInstanceState(state);
        setGridViewPadding();
    }

    private int getColCnt(int number) {
        int ret = number;
        Context context = getContext();
        int colNums = (context.getResources().getDisplayMetrics().widthPixels - getPaddingStart() - getPaddingEnd()) / getResources().getDimensionPixelSize(R.dimen.recent_grid_item_width);
        if (UiUtils.getScreenState(context) == AppConstants.PORTRAIT) {
            if (colNums >= AppConstants.HOME_RECENT_MAX_ITEM_COUNT_FOUR) {
                ret = AppConstants.HOME_RECENT_MAX_ITEM_COUNT_FOUR;
            }
        } else if (UiUtils.getScreenState(context) == AppConstants.LANDSCAPE) {
            if (colNums >= AppConstants.HOME_RECENT_MAX_ITEM_COUNT_EIGHT) {
                ret = AppConstants.HOME_RECENT_MAX_ITEM_COUNT_EIGHT;
            } else if (colNums > AppConstants.HOME_RECENT_MAX_ITEM_COUNT_FOUR && colNums < AppConstants.HOME_RECENT_MAX_ITEM_COUNT_EIGHT) {
                ret = AppConstants.HOME_RECENT_MAX_ITEM_COUNT_FOUR;
            }
        }
        return ret;
    }

    @Override
    public void setNumColumns(int numColumns) {
        super.setNumColumns(getColCnt(numColumns));
    }

    public void setGridViewPadding() {
        Context context = getContext();
        if (context != null) {
            Resources res = context.getResources();
            if (res != null) {
                int paddingLeft = (int) res.getDimension(R.dimen.recent_grid_view_padding_side);
                int paddingTop = (int) res.getDimension(R.dimen.recent_grid_view_padding_top);
                int paddingRight = (int) res.getDimension(R.dimen.recent_grid_view_padding_side);
                int paddingBottom = (int) res.getDimension(R.dimen.recent_grid_view_padding_bottom);
                setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
            }
        }
    }
}
